package com.photoeditor.demo.ui.widget.animator;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.Interpolator;

/**
 * Created by winn on 17/5/10.
 * 从指定中心position向两边散开的动画
 */

public class ScatterAnimator extends BaseItemAnimator {

    private RecyclerView mRecyclerView;

    // 启动动画的item position
    private int mCenterPositionX;

    private boolean mAnimationEnable = false;
    private Rect childRect = new Rect();
    private android.graphics.Point mPoint = new android.graphics.Point(0, 0);

    public ScatterAnimator(RecyclerView recyclerView) {
        mRecyclerView = recyclerView;
    }

    public ScatterAnimator(Interpolator interpolator) {
        mInterpolator = interpolator;
    }

    public void setPosition(int centerPositionX) {
        mCenterPositionX = centerPositionX;
    }

    public void enableAnimation(boolean enable) {
        mAnimationEnable = enable;
    }

    @Override
    protected void animateRemoveImpl(final RecyclerView.ViewHolder holder) {
        if (!mAnimationEnable) {
            return;
        }
        mRecyclerView.getChildVisibleRect(holder.itemView, childRect, mPoint);
        ObjectAnimator animator = ObjectAnimator.ofFloat(holder.itemView, "translationX",
                0, mCenterPositionX - holder.itemView.getLeft());
        animator.setDuration(getRemoveDuration()).setStartDelay(0);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                new DefaultRemoveVpaListener(holder).onAnimationEnd(holder.itemView);
            }
        });
        animator.start();
    }

    @Override
    protected void preAnimateRemoveImpl(RecyclerView.ViewHolder holder) {
        super.preAnimateRemoveImpl(holder);
    }

    @Override
    protected void preAnimateAddImpl(RecyclerView.ViewHolder holder) {
        super.preAnimateAddImpl(holder);
    }

    @Override
    protected void animateAddImpl(final RecyclerView.ViewHolder holder) {
        if (!mAnimationEnable) {
            return;
        }
        mRecyclerView.getChildVisibleRect(holder.itemView, childRect, mPoint);
        ObjectAnimator animator = ObjectAnimator.ofFloat(holder.itemView, "translationX",
                mCenterPositionX - holder.itemView.getLeft(), 0);
        animator.setDuration(getAddDuration()).setStartDelay(0);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                new DefaultAddVpaListener(holder).onAnimationEnd(holder.itemView);
            }

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                holder.itemView.setVisibility(View.VISIBLE);
            }
        });
        animator.start();

    }
}
